package com.oa.common.aspect;

import com.oa.common.annotation.OperationLog;
import com.oa.modules.approvalsec1.entity.Operationlog;
import com.oa.modules.approvalsec1.service.OperationlogService;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;


/**
 * 操作日志，切面处理类
 *
 * @author admin
 */
@Aspect
@Component
public class OperationLogAspect {
    @Autowired
    private OperationlogService operationlogService;

    @Pointcut("@annotation(com.oa.common.annotation.OperationLog)")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        //保存日志
        saveOperationLog(point);
        return result;
    }

    private void saveOperationLog(ProceedingJoinPoint joinPoint) throws JSONException {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        Operationlog operationlog = new Operationlog();
        OperationLog log = method.getAnnotation(OperationLog.class);
        String value = "";
        if (log != null) {
            //注解上的描述
            value = log.value();
        }
        //请求的参数
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            operationlog.setOperation(value);
        } else {
            try {
                HashMap params = (HashMap) args[0];
                if ("".equals(params.get("business_name")) || params.get("business_name") == null) {
                    operationlog.setOperation(value + "：查看列表");
                } else {
                    operationlog.setOperation(value + "：检索有\"" + params.get("business_name") + "\"关键字的数据");
                }
            } catch (Exception e) {

            }
        }

        //用户名
        Object username = SecurityUtils.getSubject().getPrincipal();
        JSONObject jsonObject = new JSONObject(username);
        if (jsonObject.has("userid")) {
            operationlog.setUserId(jsonObject.get("userid").toString());
        } else {
            operationlog.setUserId(jsonObject.get("userId").toString());
        }
        if (jsonObject.has("username")) {
            operationlog.setUserName(jsonObject.get("username").toString());
        } else {
            operationlog.setUserName(jsonObject.get("userName").toString());
        }
        operationlog.setOperationTime(new Date());
        //保存日志
        operationlogService.save(operationlog);
    }
}
